package com.sa.token.oauth2.demo.satokenoauth2demo.controller;

import cn.dev33.satoken.oauth2.logic.SaOAuth2Consts;
import cn.dev33.satoken.oauth2.logic.SaOAuth2Util;
import cn.dev33.satoken.oauth2.model.ClientTokenModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@RestController
public class ClientController {

    @RequestMapping("/client/checkToken")
    public String checkToken(HttpServletRequest req) {
        String clientId = req.getParameter(SaOAuth2Consts.Param.client_id);
        String clientSecret = req.getParameter(SaOAuth2Consts.Param.client_secret);
        String accessToken = req.getParameter(SaOAuth2Consts.Param.access_token);
        String scope = req.getParameter(SaOAuth2Consts.Param.scope);

        //校验 clientId clientSecret
        SaOAuth2Util.checkClientSecret(clientId, clientSecret);

        //校验token是否有效
        ClientTokenModel clientTokenModel = SaOAuth2Util.checkClientToken(accessToken);

        //检查scope是否有效
        List<String> scopes = SaFoxUtil.convertStringToList(clientTokenModel.scope);
        if(!scopes.contains(scope)){
            return "不具备scope:" + scope;
        }

        return "客户端模式："+clientTokenModel.clientId;
    }

}
